"""
Case Type   : security-EqualityQuery
Case Name   : 创建cek，当cmk不存在时合理报错
Create At   : 2022/2/19
Owner       : opentestcase003
Description :
    1.  gsql客户端-C连接数据库，创建cek，cmk不存在
Expect      :
    1.报错： ERROR(CLIENT): no global setting found in local cache
History     :
    Modified by peilinqian at 2025-1-22:优化用例配置$GAUSSHOME/etc/localkms路径步骤，排除app路径拼接错误问题
"""
import os
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class Auditing(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f'-----{os.path.basename(__file__)} start-----')
        self.sh_primy = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.userNode = Node('PrimaryDbUser')
        self.cek = 'cek_security_qualityquery_0041'
    
    def test_security(self):
        text = '----pre1:配置$GAUSSHOME/etc/localkms路径;expect:成功---'
        self.logger.info(text)
        mkdir_cmd = f'source {macro.DB_ENV_PATH};' \
                    f'if [ ! -d "$GAUSSHOME/etc/localkms" ];' \
                    f'then mkdir $GAUSSHOME/etc/localkms;fi'
        self.logger.info(mkdir_cmd)
        self.userNode.sh(mkdir_cmd).result()
        text = '---step1:ggsql客户端-C连接数据库，创建cek，cmk不存在;expect:失败---'
        self.logger.info(text)
        sql_cmd1 = f'create column encryption key {self.cek} with values ' \
            f'(client_master_key = cmk_security, ' \
            f'algorithm = aead_aes_256_cbc_hmac_sha256);'
        msg1 = self.sh_primy.execut_db_sql(sql_cmd1, sql_type='-C')
        self.logger.info(msg1)
        self.assertIn('no global setting found in local cache',
                      msg1, text + '执行失败')
    
    def tearDown(self):
        text = '----删除文件----'
        self.logger.info(text)
        clean_cmd = f"source {macro.DB_ENV_PATH};" \
                    f"rm -rf $GAUSSHOME/etc/localkms;" \
                    f"ls $GAUSSHOME/etc/localkms"
        clean_msg = self.userNode.sh(clean_cmd).result()
        self.assertIn('No such file or directory', clean_msg, text + '执行失败')
        self.logger.info(f'-----{os.path.basename(__file__)} finish-----')
